package cn.witsky.cdrService.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.witsky.cdrService.config.FtpConfig;

@RestController
public class CdrController {
	private static Logger logger = LoggerFactory.getLogger(CdrController.class);
	@Autowired
	FtpConfig ftpConfig;
	
	@RequestMapping(value = "workingTest")
	public void workingTest(HttpServletResponse resp) {
//		System.out.println(config.getHost("secret-axb-record-files.oss-cn-shanghai.aliyuncs.com"));
		// 将获取的json数据封装一层，然后在给返回
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("message", "success");
		// 写json数据到客户端
		this.writeJson(resp, result);
	}
	
	@RequestMapping(value = "/cdrService/{method}", method = RequestMethod.GET)
	public void get(HttpServletResponse resp, @PathVariable String method) {
		if (method.equals("workingTest")) {
			this.workingTest(resp);
		}else {
			JSONObject result = new JSONObject();
			result.put("code", "-1");
			result.put("message", "非法的请求.....");
			this.writeJson(resp, result);
		}
	}
////	@RequestMapping(value = "/cdrService/{method}", method = RequestMethod.POST)
////	public void post(@RequestBody JSONObject jsonParam, HttpServletResponse resp, @PathVariable String method) {
////		if (method.equals("moCall")) {
////			this.moCall(jsonParam, resp);
////		} else {
////			JSONObject result = new JSONObject();
////			result.put("code", "-1");
////			result.put("message", "非法的请求.....");
////			this.writeJson(resp, result);
////		}
////	}
//
//	@RequestMapping(value = "/moCall", method = RequestMethod.POST)
//	public void moCall(@RequestBody JSONObject jsonParam, HttpServletResponse resp) {
//		// 将获取的json数据封装一层，然后在给返回
//		logger.info("moCall reciveMsg:{}",jsonParam);
//		MoCallRequest mtCallRequest = JSON.parseObject(jsonParam.toJSONString(), MoCallRequest.class);
//		CallFile callFile=mtCallRequest.covertCallFile();
//		File datfile = null;
//		boolean isSuc =false;
//		if(callFile!=null) {
//			String filePath="moCall.dat";
//			datfile=new File(filePath);    //1、建立连接
//			DataOutputStream d =null;
//			try {
//				datfile.createNewFile();
//				d=new DataOutputStream(new FileOutputStream(datfile));
//				callFile.writeToFile(callFile, d);
//				isSuc=true;
//				logger.info("生成单数据成功");
//			}catch(Exception ex) {
//				isSuc=false;
//				logger.error("生成单数据出现异常:",ex);
//			}finally {
//				if(d!=null) {
//					try {
//						d.close();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//			}
//		}
//		if(isSuc) {
//			logger.info("开始上传话单文件......");
//			try {
//				isSuc=FTPUtil.uploadFile(ftpConfig, datfile, "test.dat");
//			} catch (Exception e) {
//				isSuc=false;
//				logger.error("ftp上传话单出现异常:",e);
//			}			
//		}
//		if(datfile!=null) {
//			datfile.delete();
//		}
//		JSONObject result = new JSONObject();
//		if (!isSuc) {
//			result.put("code", "-1");
//			result.put("message", "生成话单文件失败");
//		} else {
//			result.put("code", "0");
//			result.put("message", "生成话单文件成功");
//		}
//		// 写json数据到客户端
//		this.writeJson(resp, result);
//	}
	
	/**
	 * 功能描述:写数据到浏览器上<br/>
	 * 
	 * @param resp
	 * @param json
	 */
	private void writeJson(HttpServletResponse resp, JSONObject json) {
		PrintWriter out = null;
		try {
			// 设定类容为json的格式
			resp.setContentType("application/json;charset=UTF-8");
			out = resp.getWriter();
			// 写到客户端
			out.write(json.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}
